package org.farm2.service.controller;

import com.farm2.llm.client.handle.impl.SseEmitterHandle;
import com.farm2.llm.client.inter.Farm2EmbedClientInter;
import com.farm2.llm.client.inter.Farm2LlmClientInter;
import com.farm2.llm.client.msg.ChatMsg;
import lombok.extern.slf4j.Slf4j;
import org.farm2.base.db.process.FarmProcessTypeEnum;
import org.farm2.base.db.process.FarmProcessUtils;
import org.farm2.files.service.ResourceFileServiceInter;
import org.farm2.llm.service.LlmClientServiceInter;
import org.farm2.tools.files.Farm2ProcessState;
import org.farm2.tools.web.dto.SendMsgDto;
import org.farm2.service.utils.FarmAIMsgCacheUtils;
import org.farm2.skc.service.SkcKnowServiceInter;
import org.farm2.tools.db.commons.FarmUUID;
import org.farm2.tools.web.FarmResponseCode;
import org.farm2.tools.web.FarmResponseResult;
import org.farm2.wdap.service.WdapExtendFileServiceInter;
import org.farm2.wdap.service.WdapTaskServiceInter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.function.Consumer;

/**
 * 事务进度获取
 *
 * @author cbtg自动生成  2025-2-4 12:13:51
 */
@RestController
@RequestMapping("/api/process")
@Slf4j
public class WebProcessController {
    @Autowired
    private SkcKnowServiceInter skcKnowServiceImpl;
    @Autowired
    private WdapExtendFileServiceInter wdapExtendFileServiceImpl;
    @Autowired
    private ResourceFileServiceInter resourceFileServiceImpl;
    @Autowired
    private WdapTaskServiceInter wdapTaskServiceImpl;
    @Autowired
    private LlmClientServiceInter llmClientServiceImpl;
    private final ExecutorService executorService = Executors.newFixedThreadPool(20);

    @GetMapping("/stream")
    public SseEmitter stream(@RequestParam String key, @RequestParam String type) {
        SseEmitter emitter = new SseEmitter();
        emitter.onError(new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) {
                //错误
            }
        });
        // 模拟定时发送数据
        executorService.submit(() -> {
            Farm2ProcessState state = FarmProcessUtils.getProcess(key, FarmProcessTypeEnum.valueOf(type));
            while (state != null && !state.isEnd()) {
                Farm2ProcessState.sleep(500);
                state = FarmProcessUtils.getProcess(key, FarmProcessTypeEnum.valueOf(type));
                try {
                    emitter.send(SseEmitter.event()
                            .name("message")
                            .data(state));
                } catch (IOException e) {
                    state = null;
                    throw new RuntimeException(e);
                }
            }
            emitter.complete();
        });
        return emitter;
    }

}
